<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
require_once 'includes/connect.php';
require_once 'includes/function.php';

$db = new myDBS();

$id = $_REQUEST["id"];
$borrow_id = $_REQUEST["borrow_id"];
$task = $_REQUEST["task"];
$card_id = $_REQUEST["card_id"];
$objective = $_REQUEST["objective"];
$mati = $_REQUEST["mati"];
$mati_date = $_REQUEST["mati_date"];
$borrow_amount = $_REQUEST["borrow_amount"];
$promise_date = $_REQUEST["promise_date"];
$maturity_date = $_REQUEST["maturity_date"];
$extend_date = $_REQUEST["extend_date"];
$budget_remain = $_REQUEST["budget_remain"];
$budget_remain_this = $_REQUEST["budget_remain_this"];


switch ($task) {
    case "new":
        $sql = "INSERT INTO tbl_promise_borrow (card_id, objective, mati, mati_date"
                . " , borrow_amount, promise_date, maturity_date, extend_date, remain"
            . " , budget_remain, budget_remain_this) "
                . " VALUES ('$card_id' "
                . " , '$objective'"
                . " , '$mati'"
                . " , '$mati_date'"
                . " , '$borrow_amount'"
                . " , '$promise_date'"
                . " , '$maturity_date'"
                . " , '$extend_date' "
                . " , '$borrow_amount'"
                . " , '$budget_remain'"
                . " , '$budget_remain_this'"
                . ")";

//        echo $sql;
        $db->runSQL($sql) or die(mysql_error());

        break;

    case "edit":
        $sql = "UPDATE tbl_promise_borrow "
                . " SET card_id = '$card_id' "
                . ", objective = '$objective'"
                . ", borrow_amount = '$borrow_amount'"
                . ", promise_date = '$promise_date'"
                . ", maturity_date = '$maturity_date'"
                . ", extend_date = '$extend_date'"
                . ", remain = '$borrow_amount'"
                . ", budget_remain = '$budget_remain'"
                . ", budget_remain_this = '$budget_remain_this'"
                . " WHERE borrow_id = '$borrow_id'";
        echo $sql;
        $db->runSQL($sql) or die(mysql_error());

        break;

    case "cancel":
        $sql = "UPDATE tbl_promise_borrow "
                . " SET cancel_date = '$cancel_date' "
                . ", cancel_reason = '$cancel_reason' "
                . ", status = 'cancel' "
                . " WHERE borrow_id = '$id'";
        //echo $sql;

        $db->runSQL($sql) or die(mysql_error());
        break;

    case "bad":
        $sql = "UPDATE tbl_promise_borrow "
                . " SET bad = 'หนี้สูญ' "
                . " WHERE borrow_id = '$borrow_id'";
        //echo $sql;

        $db->runSQL($sql) or die(mysql_error());
        break;
    default:
        break;
}
?>

